/**
 * 功能:TK_GOODS Service类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:42:37
 */
package com.towker.frame.service.basicData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.towker.core.db.BaseService;
import com.towker.core.db.EntityDao;
import com.towker.frame.dao.basicData.GoodsDao;
import com.towker.frame.model.basicData.Goods;

@Service
public class GoodsService extends BaseService<Goods, java.lang.Long>
{
	private GoodsDao dao;
	
	public void setGoodsDao(GoodsDao dao)
	{
		this.dao = dao;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao()
	{
		return this.dao;
	}
	
	/**
	 * 增加或减少当前在下订单数量
	 * @param id
	 * @param amount
	 */
	public void addDownOrderAmount(Long id, double amount) {
		Goods goods = getById(id);
		if (goods != null) {
			goods.setDown_order(goods.getDown_order() + amount);
			// >0 时累计总数
			if (amount > 0) {
				goods.setDown_order_total(goods.getDown_order_total() + amount);
			}
			update(goods);
		}
	}
	
	/**
	 * 增加或减少当前库存数量
	 * @param id
	 * @param amount
	 */
	public void addStockAmount(Long id, double amount) {
		Goods goods = getById(id);
		if (goods != null) {
			goods.setStock(goods.getStock() + amount);
			update(goods);
		}
	}
	
	/**
	 * 批量修改物料单价
	 * 
	 * @param typeId
	 *            大类
	 * @param classId
	 *            小类
	 * @param goodsSn1
	 *            开始物料编码
	 * @param goodsSn2
	 *            　结束物料编码
	 * @return 
	 */
	public int batchUpdJprice(Integer typeId, Integer classId, String goodsSn1, String goodsSn2, double jprice, double tprice_value, double bprice_value, double wprice_value, double sprice_value) {
		return this.dao.batchUpdJprice(typeId, classId, goodsSn1, goodsSn2, jprice, tprice_value, bprice_value, wprice_value, sprice_value);
	}
	
	public List<Goods> getGoodsBybatch(Integer typeId, Integer classId, String goodsSn1, String goodsSn2) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (typeId != null) {
			map.put("type_id", typeId);
		}
		if (classId != null) {
			map.put("class_id", classId);
		}
		if (StringUtils.isNotBlank(goodsSn1)) {
			map.put("goods_sn1", goodsSn1);
		}
		if (StringUtils.isNotBlank(goodsSn2)) {
			map.put("goods_sn2", goodsSn2);
		}
		return getList(map);
	}
	public boolean checkExistsCode(String goodsSn, Integer id){
		return dao.checkExistsCode(goodsSn, id);
	}
	public Goods getByGoodsSn(String goodsSn){
		return dao.getByGoodsSn(goodsSn);
	}
}
